package com.wandian.setting.mvvm.usercenter.sdpagefragment;

import android.app.Application;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.mylib.libcore.bean.BaseBean;
import com.mylib.libcore.mvvm.BaseViewModel;
import com.mylib.libcore.network.rx.RxResultHelper;
import com.mylib.libcore.utils.JsonUtil;
import com.mylib.libcore.utils.RetrofitUtil;
import com.mylib.libcore.utils.ToastUtil;
import com.mylib.libcore.utils.UIUtils;
import com.mylib.libcore.utils.crc.AESUtils;
import com.wandian.setting.bean.DynamicDataBean;
import com.wandian.setting.data.BaseAppApi;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import static com.mylib.libcore.config.NetConfig.BASE_SUCCESS_CODE;

/**
 * @author Administrator
 * @date 2019/12/16-13:32
 * @description SDPageFragmentViewModel
 **/
public class SDPageFragmentViewModel extends BaseViewModel {
    private BaseAppApi baseAppApi;

    public MutableLiveData<List<DynamicDataBean>> listSD =new MutableLiveData<>();
    public SDPageFragmentViewModel(@NonNull Application application) {
        super(application);
        baseAppApi = RetrofitUtil.getAppRetrofit().create(BaseAppApi.class);
    }

    //获取树洞
    public void getSDList(String pageNum,String pageSize ){
        showDialog();
        JsonObject jsonObject = new JsonObject();
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("pageNum", pageNum);
        hashMap.put("pageSize", pageSize);
        jsonObject.addProperty("wdk", AESUtils.getInstance().encrypt(hashMap));
        RxResultHelper.getHttpObservable(getLifecycleProvider(), baseAppApi.getSDList(jsonObject))
                .subscribe(new Observer<BaseBean<Object>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }
                    @Override
                    public void onNext(BaseBean<Object> bean) {
                        dismissDialog();

                        Log.d("getSDList",   JsonUtil.toJson(bean));
                        if(!UIUtils.isEmpty(bean) && bean.getCode()==BASE_SUCCESS_CODE){
                            String json = AESUtils.getInstance().decrypt(bean.getData())+"";
                            Log.d("getDTList",   json);
                            if(!UIUtils.isEmpty(json) && json.length()>4){
                                List<DynamicDataBean> listBean = JsonUtil.fromJson(json, new TypeToken<ArrayList<DynamicDataBean>>() {});
                                if(listBean != null &&listBean.size()>0){
                                    listSD.setValue(listBean);
                                }
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        dismissDialog();
                        Log.d("getSDList",   JsonUtil.toJson(e));
                        if(!UIUtils.isEmpty(e.getMessage())){
                            ToastUtil.show(e.getMessage());
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }
    //删除树洞
    public void deleteSD(String id, BaseQuickAdapter adapter,int index){
        showDialog();
        JsonObject jsonObject = new JsonObject();
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("id", id);
        jsonObject.addProperty("wdk", AESUtils.getInstance().encrypt(hashMap));
        RxResultHelper.getHttpObservable(getLifecycleProvider(), baseAppApi.deleteSD(jsonObject))
                .subscribe(new Observer<BaseBean<Object>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }
                    @Override
                    public void onNext(BaseBean<Object> bean) {
                        dismissDialog();
                        Log.d("deleteSD",   JsonUtil.toJson(bean));
                        if(!UIUtils.isEmpty(bean) && bean.getCode()==BASE_SUCCESS_CODE){
                            ToastUtil.show("删除成功");
                            if(!UIUtils.isEmpty(adapter))adapter.remove(index);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        dismissDialog();
                        Log.d("deleteSD",   JsonUtil.toJson(e));
                        if(!UIUtils.isEmpty(e.getMessage())){
                            ToastUtil.show(e.getMessage());
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }

}

